var animation;
const request = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: "",
    isUser: true, //用户与管理员区别
    isRegistered: "", //注册于未注册的
    maskFlag: true, //隐藏的选项框
    isFinished: false, //信息是否完善
    isAuth: "", //onload时是否登录或者注册
    userInfo: "", //用户信息
    isLock: false, //是否被锁定
  },
  login: function (s) {
    var that = this;
    wx.login({
      success: res => {
        var data = {
          code: res.code
        }
        request.miniLogin(data).then(res => {
          if (res.data.code == 200) {
            console.log(res);
            // token: res.data.data.token;
            var userInfo = res.data.data.wxUser;
            userInfo.token = res.data.data.token;
            wx.setStorageSync('userInfo', userInfo);
            that.data.isAuth = true;
            that.data.userInfo = userInfo;
            // 判断用户信息是否完善
            // if (userInfo.idCard === null) {
            //   that.setData({
            //     isFinished: false
            //   })
            //   console.log("登录此未完善信息")
            // } else {
            //   that.setData({
            //     isFinished: true
            //   })
            //   console.log("登录此已完善信息")
            // }
            // 判断用户时检察人员还是住户
            if (userInfo.isProsecutor == 1) {
              that.setData({
                isUser: false,
              })
            } else {
              that.setData({
                isUser: true,
              })
            }
            //跳转到对应界面
            if (s != "" && userInfo.idCard != null) {
              wx.hideLoading();
              wx.navigateTo({
                url: s,
              })
            } else if (s != "" && userInfo.idCard == null) {
              wx.showToast({
                title: '请完善个人信息',
                icon: 'none',
              })
            }
          } else if (res.data.code == 4) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 3000,
            })
            that.data.isLock = true;
          } else {
            //登陆不成功就去注册
            that.Register(s);
          }
        })
      },
      fail: res => {
        console.log(res);

      }
    })
  },
  Register: function (s) {
    var that = this;
    wx.login({
      success: allres => {
        wx.getUserInfo({
          success: res => {
            var data = {
              code: allres.code,
              iv: res.iv,
              encryptedData: res.encryptedData
            }
            request.miniRegister(data).then(function (res) {
              console.log(res);
              if (res.data.code == 200) {
                var userInfo = res.data.data.wxUser;
                userInfo.token = res.data.data.token;
                wx.setStorageSync('userInfo', userInfo);
                that.data.userInfo = userInfo;
                //检查用户信息是否为空
                if (userInfo.idCard === null) {
                  that.setData({
                    isFinished: false
                  })
                } else {
                  that.setData({
                    isFinished: true
                  })
                }
                //刚注册的人都是住户
                that.setData({
                  isUser: true,
                })
                that.data.isAuth = true;
                //跳转到相应界面
                if (s != "" && userInfo.idCard != null) {
                  wx.hideLoading();
                  wx.navigateTo({
                    url: s,
                  })
                } else if (s != "" && userInfo.idCard == null) {
                  wx.showToast({
                    title: '请完善个人信息',
                    icon: 'none',
                  })
                }
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                })
              }
            })
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 显示本地的屏幕高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          screenHeight: res.screenHeight - 44 - res.statusBarHeight
        })
      },
    })
    wx.getSetting({
      complete: (res) => {
        if (res.authSetting['scope.userInfo']) {
          console.log("已经授权");
          that.data.isAuth = true;
          //如果授权了就去登录/注册
          that.login('');
        } else {
          console.log("未授权");
          that.data.isAuth = false;
        }
      },
    })
    //动画初始化
    animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })

  },

  //个人信息
  gotoMessage: function () {
    var that = this;
    if (that.data.isLock == false) {
      wx.showLoading({
        title: '加载中',
      })
      wx.getSetting({
        complete: (res) => {
          if (res.authSetting['scope.userInfo']) {
            console.log("已经授权");
            //授权后往下走，
            if (that.data.isAuth == false) {
              console.log("未登录/注册");
              that.login("/pages/message/message");
            } else {
              wx.navigateTo({
                url: '/pages/message/message',
              })
              wx.hideLoading()
            }
          } else {
            console.log("未授权");
            wx.hideLoading();
          }
        },
      })
    } else {
      wx.showToast({
        title: "账号异常",
        icon: 'none',
        duration: 3000,
      })
    }

  },
  //展示二维码
  gotoCode: function () {
    var that = this;
    if (that.data.isLock == false) {
      wx.showLoading({
        title: '加载中',
      })
      wx.getSetting({
        complete: (res) => {
          if (res.authSetting['scope.userInfo']) {
            console.log("已经授权");
            //授权后往下走，
            if (that.data.isAuth == false) {
              console.log("未登录/注册");
              that.login("/pages/code/code");
            } else {
              console.log(that.data.isFinished);
              if (that.data.isFinished == false) {
                wx.showToast({
                  title: '请完善信息',
                  icon: 'none',
                })
              } else {
                wx.navigateTo({
                  url: '/pages/code/code',
                })
              }

              wx.hideLoading()
            }
          } else {
            console.log("未授权");
            wx.hideLoading();
          }
        },
      })
    } else {
      wx.showToast({
        title: "账号异常",
        icon: 'none',
        duration: 3000,
      })
    }

  },
  openSelect: function () {
    // this.setData({
    //   maskFlag: false,
    // })
    // animation.translateY(-175).step()
    // this.setData({
    //   animationData: animation.export()
    // })
    var that = this;
    if (that.data.isLock == false) {
      wx.showLoading({
        title: '加载中',
      })
      wx.getSetting({
        complete: (res) => {
          if (res.authSetting['scope.userInfo']) {
            console.log("已经授权");
            //授权后往下走，
            if (that.data.isAuth == false) {
              console.log("未登录/注册");
              that.login('');
              setTimeout(function () {
                wx.hideLoading();
                this.setData({
                  maskFlag: false,
                })
                animation.translateY(-175).step()
                this.setData({
                  animationData: animation.export()
                })
              }, 2000)
            } else {
              if (that.data.isFinished == false) {
                wx.showToast({
                  title: '请完善信息',
                  icon: 'none',
                })
              } else {
                wx.hideLoading()
                this.setData({
                  maskFlag: false,
                })
                animation.translateY(-175).step()
                this.setData({
                  animationData: animation.export()
                })
              }

            }
          }
        }
      })
    } else {
      wx.showToast({
        title: "账号异常",
        icon: 'none',
        duration: 3000,
      })
    }


  },
  close: function () {
    animation.translateY(175).step()
    this.setData({
      animationData: animation.export(),
      maskFlag: true
    })
  },
  //扫码
  gotoScanCode: function () {
    var that = this;
    // 打开扫码
    wx.scanCode({
      success: (res) => {
        console.log(res.result)
        that.setData({
          maskFlag: true,
        })
        wx.navigateTo({
          url: '/pages/edit/edit?result=' + res.result,
        })
        wx.hideLoading();
      },
      fail: (res) => {
        console.log(res);
        that.setData({
          maskFlag: true,
        })
        wx.hideLoading();
      }
    })
  },
  //信息记录
  gotoRecord: function () {

    var that = this;
    if (that.data.isLock == false) {
      wx.getSetting({
        complete: (res) => {
          if (res.authSetting['scope.userInfo']) {
            console.log("已经授权");
            //授权后往下走，
            if (that.data.isAuth == false) {
              console.log("未登录/注册");
              that.login("/pages/record/record");
            } else {
              if (that.data.userInfo.isProsecutor == true) {
                wx.navigateTo({
                  url: '/pages/record/record',
                })
              } else {
                wx.showToast({
                  title: '系统显示您不是检察人员,请联系管理员',
                  icon: 'none',
                })
              }
            }
          }
        }
      })
    } else {
      wx.showToast({
        title: "账号异常",
        icon: 'none',
        duration: 3000,
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    var info = wx.getStorageSync('userInfo');
    // request.getUser(info.token).then(function(res){
    //   if(res.code==200){
    //     console.log(res);
    //   }
    // })
    var status=wx.getStorageSync('status');
    console.log(status);
    if (status === null||status==0||status==2) {
      this.setData({
        userInfo: info,
        isFinished: false,
      })
      that.data.isFinished=false;
      console.log("未完善")
    } else {
      this.setData({
        userInfo: info,
        isFinished: true,
      })
      console.log("完善")
    }
    // request.getExamine(info.id,this.data.userInfo.token).then(res=>{
    //   console.log(res);
    //   if(res.data.code==200){
    //     that.setData({
    //       status:res.data.data.status
    //     })
    //     console.log(res.data)
    //     wx.setStorageSync('status',res.data.data.status)
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})